
<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="formInline.search1"
          size="mini"
          placeholder="输入书名查询"
          v-on:input="handleSearch()"
        >
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-input
          v-model="formInline.search2"
          size="mini"
          placeholder="输入书籍类型查询"
          v-on:input="handleSearch()"
        >
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-button
          icon="el-icon-circle-plus-outline"
          type="text"
          @click="search = true"
          >查询</el-button
        >
      </el-form-item>

      <el-form-item>
        <el-button
          icon="el-icon-circle-plus-outline"
          type="text"
          @click="dialogAdd = true"
          >添加</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-delete" type="text" @click="handleDeleteList()"
          >删除</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTable"
      :data="tableData"
      border
      highlight-current-row
      style="width: 100%"
      @selection-change="handleSelectionDelete"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="书籍编号">
        <template slot-scope="scope">
          <span>{{ scope.row.bookNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" prop="bookType"></el-table-column>
      <el-table-column label="出版日期" prop="bookDate"></el-table-column>
      <el-table-column label="作者">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="right">
            <p>类型: {{ scope.row.bookType }}</p>
            <p>书名: {{ scope.row.bookName }}</p>
            <p>价格{{ scope.row.bookPrice }}</p>
            <p>作者: {{ scope.row.bookAuthor }}</p>
            <p>出版日期: {{ scope.row.bookDate }}</p>
            <p>书籍编号: {{ scope.row.bookNumber }}</p>
            <div slot="reference" class="name-wrapper">
              <el-button type="text">{{ scope.row.bookAuthor }}</el-button>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="价格" prop="bookPrice"></el-table-column>
      <el-table-column label="书籍名称" prop="bookName"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="添加"
      :append-to-body="true"
      :visible.sync="dialogAdd"
      :before-close="handleClose"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
        class="demo-ruleForm"
        size="medium"
      >
        <el-form-item label="书籍名称">
          <el-input v-model="ruleForm.bookName"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="ruleForm.bookPrice"></el-input>
        </el-form-item>
        <el-form-item label="出版日期">
          <el-input v-model="ruleForm.bookDate"></el-input>
        </el-form-item>
        <el-form-item label="作者">
          <el-select
            size="medium"
            v-model="ruleForm.bookAuthor"
            placeholder="请选择作者"
          >
            <el-option
              v-for="item in authorList"
              :key="item.id"
              :label="item.label"
              :value="item.authorName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型">
          <el-select
            v-model="ruleForm.bookType"
            placeholder="请选择分类"
            prop="bookType"
          >
            <el-option label="科幻" value="科幻"></el-option>
            <el-option label="悬疑" value="悬疑"></el-option>
            <el-option label="魔改" value="魔改"></el-option>
            <el-option label="益智" value="益智"></el-option>
            <el-option label="三体" value="三体"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="emptyBookData()" size="medium">取 消</el-button>
        <el-button @click="addBook('ruleForm')" type="primary" size="medium"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <el-dialog
      title="编辑"
      :append-to-body="true"
      :visible.sync="dialogUpdate"
      :before-close="handleClose"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="70px"
        class="demo-ruleForm"
        size="medium"
      >
        <el-form-item label="书籍名称">
          <el-input v-model="ruleForm.bookName"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="ruleForm.bookPrice"></el-input>
        </el-form-item>
        <el-form-item label="出版日期">
          <el-input v-model="ruleForm.bookDate"></el-input>
        </el-form-item>
        <el-form-item label="作者">
          <el-input v-model="ruleForm.bookAuthor" readonly></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-select
            v-model="ruleForm.bookType"
            placeholder="请选择分类"
            prop="bookType"
          >
            <el-option label="科幻" value="科幻"></el-option>
            <el-option label="悬疑" value="悬疑"></el-option>
            <el-option label="魔改" value="魔改"></el-option>
            <el-option label="益智" value="益智"></el-option>
            <el-option label="三体" value="三体"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer"
        ><el-button @click="emptyBookData()" size="medium">取 消</el-button>
        <el-button @click="updateBook()" type="primary" size="medium"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <br />

    <el-pagination
      background
      :disabled="disablePage"
      :current-page.sync="currentPage"
      small
      layout="total,prev, pager, next,jumper, sizes"
      :page-size="pageSize"
      :page-sizes="[1, 5, 10, 15]"
      :total="total"
      @current-change="handlePageChange"
      @size-change="handleSizeChange"
    >
    </el-pagination>

    <router-link to="/author">Author</router-link>
  </div>
</template>

<script>
/* eslint-disable */

export default {
  name: "BookHome",
  data() {
    return {
      ruleForm: {
        bookNumber: null, //书籍id
        bookType: null, //书籍类型
        bookName: null, //书籍名称
        bookAuthor: null, //作者
        bookPrice: 25, //价格
        bookDate: null, //出版日期
      },
      rules: {},
      tableData: [],
      authorList: [],
      formInline: {
        search1: "",
        search2: "",
      },
      dialogAdd: false,
      dialogUpdate: false,
      pageSize: 5,
      currentPage: 1,
      total: 0,
      disablePage: false,
      multipleSelection: [],
    };
  },

  created() {
    this.selectAuthorList();
    this.handlePageChange();
    this.getRowCount();
  },

  methods: {
    /**
     * 分页
     */
    handlePageChange() {
      let postData = this.qs.stringify({
        pageSize: this.pageSize,
        page: this.currentPage,
        bookName: this.formInline.search1,
        bookType: this.formInline.search2,
      });
      console.log(this.currentPage);
      this.$axios({
        method: "post",
        url: "/api/queryBookListByPage",
        data: postData,
      })
        .then((response) => {
          console.log(response);
          this.tableData = response.data;
        })
        .catch((error) => {
          console.log(error);
        });
    },

    /**
     * 每页显示条数
     */

    handleSizeChange(val) {
      this.pageSize = val;
      this.handlePageChange();
    },



    /**
     * 统计书籍总数
     */
    getRowCount() {
      let postData = this.qs.stringify({
        bookName: this.formInline.search1,
        bookType: this.formInline.search2,
      });
      this.$axios({
        method: "post",
        url: "/api/getRowCount",
        data: postData,
      })
        .then((response) => {
          this.total = response.data;
          console.log(this.total);
        })
        .catch((error) => {
          console.log(error);
        });
    },

    /**
     * 添加书籍
     */
    addBook() {
      if (
        this.ruleForm.bookType == null ||
        this.ruleForm.bookName == null ||
        this.ruleForm.bookPrice == null ||
        this.ruleForm.bookDate == null ||
        this.ruleForm.bookAuthor == null
      ) {
        this.$alert("书籍信息不完整请检查", "温馨提示", {
          confirmButtonText: "确定",
        });
        return;
      }
      let postData = this.qs.stringify({
        bookType: this.ruleForm.bookType, //书籍类型
        bookName: this.ruleForm.bookName, //书籍名称
        bookPrice: this.ruleForm.bookPrice, //书籍价格
        bookDate: this.ruleForm.bookDate, //出版日期
        bookAuthor: this.ruleForm.bookAuthor, //作者
      });
      this.$axios({
        method: "post",
        url: "/api/addBook",
        data: postData,
      })
        .then((response) => {
          this.handlePageChange();
          this.getRowCount();
          this.$message({
            type: "success",
            message: "已添加!",
          });
          this.emptyBookData();
        })
        .catch((error) => {
          console.log(error);
        });
    },
    search() {
      this.handlePageChange();
      this.getRowCount();
    },

    handleSearch() {
      this.handlePageChange();
      this.getRowCount();
    },

    handleEdit(index, row) {
      this.dialogUpdate = true;
      this.ruleForm = Object.assign({}, row, index);
    },

    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
          this.emptyBookData();
        })
        .catch((_) => {});
    },

    /**
     * 清空绑定数据
     */
    emptyBookData() {
      this.dialogAdd = false;
      this.dialogUpdate = false;
      this.ruleForm = {
        bookNumber: null, //书籍id
        bookType: null, //书籍类型
        bookName: null, //书籍名称
        bookAuthor: null, //作者
        bookPrice: 25, //价格
        bookDate: null, //出版日期
      };
    },

    /**
     * 根据 bookNumber 删除用户
     * @param index
     * @param row
     */
    handleDelete(index, row) {
      this.$confirm("删除操作, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let postData = this.qs.stringify({
            bookNumber: row.bookNumber,
          });
          this.$axios({
            method: "post",
            url: "/api/deleteBookByNumber",
            data: postData,
          })
            .then((response) => {
              this.getRowCount();
              this.handlePageChange();

              this.$message({
                type: "success",
                message: "删除成功!",
              });
            })
            .catch((error) => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    handleSelectionDelete(val) {
      console.log(val);
      this.multipleSelection = val;
    },

    /**
     * 根据 Id 批量删除书籍
     */
    handleDeleteList() {
      let Ids = "";
      this.multipleSelection.forEach((item) => {
        Ids += item.id + ",";
      });

      this.$confirm("删除操作, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let postData = this.qs.stringify({
            idList: Ids,
          });
          console.log(postData);
          this.$axios({
            method: "post",
            url: "/api/deleteBookByIds",
            data: postData,
          })
            .then((response) => {
              this.getRowCount();
              this.handlePageChange();

              this.$message({
                type: "success",
                message: "删除成功!",
              });
            })
            .catch((error) => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    updateBook() {
      if (
        this.ruleForm.bookType == null ||
        this.ruleForm.bookName == null ||
        this.ruleForm.bookPrice == null ||
        this.ruleForm.bookDate == null ||
        this.ruleForm.bookAuthor == null
      ) {
        this.$alert("书籍信息不完整请检查", "温馨提示", {
          confirmButtonText: "确定",
        });
        return;
      }
      let postData = this.qs.stringify({
        bookNumber: this.ruleForm.bookNumber, //书籍编号
        bookType: this.ruleForm.bookType, //书籍类型
        bookName: this.ruleForm.bookName, //书籍名称
        bookPrice: this.ruleForm.bookPrice, //书籍价格
        bookDate: this.ruleForm.bookDate, //出版日期
        bookAuthor: this.ruleForm.bookAuthor, //作者
      });
      this.$axios({
        method: "post",
        url: "/api/updateBook",
        data: postData,
      })
        .then((response) => {
          this.handlePageChange();
          this.getRowCount();
          this.$message({
            type: "success",
            message: "已编辑!",
          });
          this.emptyBookData();
        })
        .catch((error) => {
          console.log(error);
        });
    },

    /**
     * 获取作者列表
     */
    selectAuthorList() {
      this.$axios({
        method: "post",
        url: "/api/selectAuthorList",
      })
        .then((response) => {
          console.log(response.data)
          this.authorList = response.data;

        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
/* eslint-disable */
</script>
<style scoped>
</style>
